/*  This file is part of Phantom Power.
 *  Copyright (C) 2010 Jacob Dawid
 *
 *  Phantom Power is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Phantom Power is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Phantom Power. If not, see <http://www.gnu.org/licenses/>.
 */

#include "wifiDialog.h"

wifiDialog::wifiDialog()
{
	settings=new QSettings("phantom-power.configuration", QSettings::IniFormat);
	setupUserInterface();
}

wifiDialog::~wifiDialog()
{
	settings->setValue("wifi/ip1", ipAddress[0]->toPlainText().toInt());
	settings->setValue("wifi/ip2", ipAddress[1]->toPlainText().toInt());
	settings->setValue("wifi/ip3", ipAddress[2]->toPlainText().toInt());
	settings->setValue("wifi/ip4", ipAddress[3]->toPlainText().toInt());
	settings->setValue("wifi/port", ipAddress[4]->toPlainText().toInt());
	delete settings;
}

void wifiDialog::setupUserInterface()
{
	QLabel* label;
	ipAddress[0]=new QTextEdit(this);
	ipAddress[0]->setGeometry(10, 10, 40, 30);
	ipAddress[1]=new QTextEdit(this);
	ipAddress[1]->setGeometry(60, 10, 40, 30);
	ipAddress[2]=new QTextEdit(this);
	ipAddress[2]->setGeometry(110, 10, 40, 30);
	ipAddress[3]=new QTextEdit(this);
	ipAddress[3]->setGeometry(160, 10, 40, 30);
	ipAddress[4]=new QTextEdit(this);
	ipAddress[4]->setGeometry(210, 10, 50, 30);

	ipAddress[0]->setTabChangesFocus(true);
	ipAddress[1]->setTabChangesFocus(true);
	ipAddress[2]->setTabChangesFocus(true);
	ipAddress[3]->setTabChangesFocus(true);
	ipAddress[4]->setTabChangesFocus(true);

	ipAddress[0]->setText(settings->value("wifi/ip1").toString());
	ipAddress[1]->setText(settings->value("wifi/ip2").toString());
	ipAddress[2]->setText(settings->value("wifi/ip3").toString());
	ipAddress[3]->setText(settings->value("wifi/ip4").toString());
	ipAddress[4]->setText(settings->value("wifi/port").toString());

	for(int j=0;j<4;j++)
	{
		label=new QLabel(this);
		label->setGeometry(52+j*50, 10, 5, 25);
		label->setText(j<3?".":":");
	}

	_connectButton=new QPushButton(this);
	_connectButton->setGeometry(10, 75, 90, 30);
	_connectButton->setText("Connect");
	connectionState=false;

	// Catch the clicked-Signal.
	connect(_connectButton, SIGNAL(clicked()), this, SLOT(connectionButtonTriggered()));

	setTabOrder(ipAddress[0], ipAddress[1]);
	setTabOrder(ipAddress[1], ipAddress[2]);
	setTabOrder(ipAddress[2], ipAddress[3]);
	setTabOrder(ipAddress[3], ipAddress[4]);
	setTabOrder(ipAddress[4], _connectButton);
	setTabOrder(_connectButton, ipAddress[0]);
}

void wifiDialog::connectionButtonTriggered()
{
	// Check the current connectionState and decid whether to establish or cut the connection.
	if(!connectionState)
		emit establishConnection(ipAddress[0]->toPlainText().toInt(),
						     ipAddress[1]->toPlainText().toInt(),
						     ipAddress[2]->toPlainText().toInt(),
						     ipAddress[3]->toPlainText().toInt(),
						     ipAddress[4]->toPlainText().toInt());
	else
		emit cutConnection();

	connectionState=!connectionState;

	// Set the text depending on the connectionState. If connected, display "Disconnect" and vice versa.
	_connectButton->setText(connectionState?"Disconnect":"Connect");
}

void wifiDialog::synchronize()
{
	settings->sync();
}
